package edu.mse.camel.client.uml.collab.handlers;

import edu.mse.camel.client.core.events.model.ModelUpdateEvent;
import edu.mse.camel.client.core.events.model.ModelUpdateEvent2;
import edu.mse.camel.client.eventbus.pubsub.EventManager;
import edu.mse.camel.client.eventbus.pubsub.ISubscriber;
import edu.mse.camel.client.events.EventIDs;
import edu.mse.camel.client.events.IEvent;
import edu.mse.camel.client.events.peer.ReceiveGenericPeerEvent;

import edu.mse.camel.client.log.Debug;
import edu.mse.camel.client.uml.collab.CollabPlugin;

public class ModelUpdateEventHandler implements ISubscriber {

	public ModelUpdateEventHandler() {
		EventManager.getInstance().subscribeTo(EventIDs.RECEIVE_PEER_EVENT, this);
	}
	
	@Override
	public void notifyEvent(IEvent event) {
		if (event instanceof ReceiveGenericPeerEvent) {
			ReceiveGenericPeerEvent genericEvent = (ReceiveGenericPeerEvent) event;
			if ( genericEvent.getDataType().equals(ModelUpdateEvent2.class.getCanonicalName())) {
				ModelUpdateEvent2 me = genericEvent.readObject(ModelUpdateEvent2.class);
				Debug.log_info(CollabPlugin.PLUGIN_ID, "Receiving " +me);
				EventManager.getInstance().dispatchEventAsynchronous(me);
			}
			if ( genericEvent.getDataType().equals(ModelUpdateEvent.class.getCanonicalName())) {
				ModelUpdateEvent me = genericEvent.readObject(ModelUpdateEvent.class);
				EventManager.getInstance().dispatchEventAsynchronous(me);
			}
			else {
				// it should be okay that we do not understand the actual data type of a generic event
			}
		}
	}

}
